package edu.seu.mart.dao.impl;

import edu.seu.mart.dao.BaseDao;
import edu.seu.mart.dao.IGoodsDao;
import edu.seu.mart.entity.Pager;
import edu.seu.mart.entity.SystemRequest;
import edu.seu.mart.entity.SystemRequestHolder;
import edu.seu.mart.model.Goods;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zj on 2015/3/11.
 */
@Repository("goodsDao")
public class GoodsDao extends BaseDao<Goods> implements IGoodsDao {

    @Override
    public Pager<Goods> findGoods() {



        return   this.find("from Goods where status <> 2 and status <> 3");
    }

    @Override
    public Pager<Goods> findGoods(String name) {
        return this.find("from Goods where name = '" + name + "'");
    }

    @Override
    public Pager<Goods> findGoodsByNameOrBrand(String name) {
        return this.find("from Goods where name like '%" + name + "%' or brand like '%" + name + "%'");
    }

    @Override
    public Pager<Goods> findGoodsByCategory(String name) {
        if (name != null && !"".equals(name.trim())) {
            return this.find("from Goods where category = '" + name + "' and status=1");
        }else{
            return this.find("from Goods where  status=1");
        }

    }

    @Override
    public Pager<Goods> findGoods(int i) {
        return this.find("from Goods where status = " + i + "");
    }

    @Override
    public Pager<Goods> findGoodsListBySellerId(int sellerId, String flag) {
        if (flag == null) {
            return this.find("from Goods where seller.id =" + sellerId);
        } else {
            return this.find("from Goods where seller.id = " + sellerId + " and status = " + Integer.valueOf(flag));
        }
    }

    @Override
    public Pager<Goods> findGoodsByCategory(String category, String brand, String price, String cpu, String saleOrder, String priceOrder) {


        if(category==null&&brand==null&&price==null&&cpu==null&&saleOrder==null&&priceOrder==null){
            return this.find("from Goods");
        }else{
            StringBuffer queryStr= new StringBuffer("from Goods where ");
            if(category!=null){
                queryStr.append("category='"+category+"'  ");
            }
            if(brand!=null){
                queryStr.append("and brand='"+brand+"' ");
            }
            if(price!=null){
                String[] str = price.split("-");
                double cost1 = Double.parseDouble(str[0]);
                double cost2 = Double.parseDouble(str[1]);
                queryStr.append("and  price between "+cost1+ " and "+cost2+" ");
            }
            if(cpu!=null){
                queryStr.append(" and  cpu='"+cpu+"' ");
            }
            if(saleOrder!=null){
                queryStr.append("order by salesnum "+saleOrder);
            }
            if(priceOrder!=null){
                queryStr.append("order by price "+priceOrder);
            }
            System.out.println(queryStr.toString());
            return this.find(queryStr.toString());
        }


    }

    @Override
    public List<Goods> findAllGoods() {
        return this.list("from Goods where status =1");
    }

    @Override
    public Pager<Goods> findMatchedGoods(String query,String category,String saleOrder,String priceOrder) {
  
        SystemRequest systemRequest = new SystemRequest();
        if(saleOrder!=null){
            systemRequest.setOrder(saleOrder);
            systemRequest.setSort("salesnum");
        }
        if(priceOrder!=null){
            systemRequest.setOrder(priceOrder);
            systemRequest.setSort("price");
        }

        SystemRequestHolder.initRequestHolder(systemRequest);
        Map<String, Object> alias = new HashMap<String, Object>();
        if (query.contains("-")) {
            String[] str = query.split("-");
            double cost1 = Double.parseDouble(str[0]);
            double cost2 = Double.parseDouble(str[1]);
            alias.put("cost1", cost1);
            alias.put("cost2", cost2);
            String hql = "from Goods where price between (:cost1) and (:cost2) and category='"+category+"'";
            return super.find(hql, alias);
        }
        alias.put("brand", "%" + query + "%");
        alias.put("CPU", "%" + query + "%");
        String hql = "from Goods where brand like (:brand) or CPU like (:CPU) and category='"+category+"'";
        return super.find(hql, alias);
    }

    @Override
    public List<Goods> findRecommendGoods(String query, String category) {
        SystemRequest systemRequest = new SystemRequest();
        systemRequest.setPageOffset(0);
        systemRequest.setPageSize(7);
        SystemRequestHolder.initRequestHolder(systemRequest);
        Map<String, Object> alias = new HashMap<>();
        if (query.contains("-")) {
            String[] str = query.split("-");
            double cost1 = Double.parseDouble(str[0]);
            double cost2 = Double.parseDouble(str[1]);
            alias.put("cost1", cost1);
            alias.put("cost2", cost2);
            String hql = "from Goods where price between (:cost1) and (:cost2) and category = '" + category + "'";
            return super.find(hql, alias).getDatas();
        }
        alias.put("brand", "%" + query + "%");
        String hql = "from Goods where brand like (:brand) and category = '" + category + "'";
        return super.find(hql, alias).getDatas();
    }
}
